<?php

namespace backend\controllers;


use common\models\Order;
use common\models\Shop;
use common\models\ShopAddress;
use common\models\ShopUser;
use common\models\SupplierGoods;
use common\service\ConstantService;
use common\service\GlobalUrlService;

class ShopUserController extends BaseController
{
    /**
     * 供货商列表
     * @return mixed|string
     * @throws \yii\base\ExitException
     */
    public function actionIndex(){
        if($this->isAjax() && $this->isGet()){
            $user_name = $this->get("user_name");
            $query = ShopUser::find();
            if(!empty($user_name)){
                $query->andWhere(['like','nickname',base64_encode($user_name)]);
            }
            $p = intval( $this->get('page',1) );
            $this->page_size = intval( $this->get('limit',10) );
            $p = ( $p > 0 )? $p : 1;
            $count = $query->count();

            $offset = ($p - 1) * $this->page_size;

            $users = $query->limit($this->page_size)->offset($offset)->asArray()->all();

            if(!empty($users)){
                $new_orders = [];
                $new_address = [];
                $new_shop = [];
                $user_ids = array_column($users,'id');
                $shop_id = array_column($users,'shop_id');
                if(!empty($user_ids)){
                    $orders = Order::find()
                        ->select(['user_id','count(id) as order_num'])
                        ->where(['in','user_id',$user_ids])
                        ->groupBy('user_id')
                        ->asArray()
                        ->all();

                    foreach ($orders as $order){
                        $new_orders[$order['user_id']] = $order;
                    }
                }

                if(!empty($shop_id)){
                    $shop = Shop::find()
                        ->select(['id','shop_name'])
                        ->where(['id'=>$shop_id])
                        ->asArray()
                        ->all();
                    foreach ($shop as $item){
                        $new_shop[$item['id']] = $item['shop_name'];
                    }
                    unset($item);

                }

                $address_ids = array_column($users,'address_id');
                if(!empty($address_ids)){
                    $address = ShopAddress::find()->where(['id'=>$address_ids])->asArray()->all();

                    foreach ($address as $addre){
                        $new_address[$addre['id']] = $addre;
                    }
                }


                foreach ($users as &$user){
                    $user['nickname'] = base64_decode($user['nickname']);
                    $user['avatarurl'] = GlobalUrlService::buildCdnUrl($user['avatarurl']);
                    $user['order_num'] = 0;
                    $user['address'] = '';
                    $user['mobile'] = '';
                    $user['shop_name'] = '';
                    if(isset($new_orders[$user['id']]['order_num']) && !empty($new_orders[$user['id']]['order_num']) ){
                        $user['order_num'] = $new_orders[$user['id']]['order_num'];
                    }
                    if(isset($new_shop[$user['shop_id']]) && !empty($new_shop[$user['shop_id']]) ){
                        $user['shop_name'] = $new_shop[$user['shop_id']];
                    }
                    if(isset($new_address[$user['address_id']]) && !empty($new_address[$user['address_id']])){
                        $user['address'] = $new_address[$user['address_id']]['province']
                            .' '.$new_address[$user['address_id']]['city']
                            .' '.$new_address[$user['address_id']]['area']
                            .' '.$new_address[$user['address_id']]['district'];
                        $user['mobile'] = $new_address[$user['address_id']]['mobile'];
                    }

                }
            }
            return $this->renderPageJson($users,$count,ConstantService::$response_tab_success);
        }
        return $this->render('index');
    }

    /**
     * 用户详情页
     * @return string
     */
    public function actionInfo(){
        $id=$this->get('id');
        $data=ShopUser::find()->where(['id'=>$id])->asArray()->one();
        $data['nickname'] = base64_decode($data['nickname']);
        $data['avatarurl'] = GlobalUrlService::buildCdnUrl($data['avatarurl']);

        return $this->render('info',['data'=>$data]);
    }

    /**
     * 获取订单
     * @return mixed
     * @throws \yii\base\ExitException
     */
    public function actionGetOrder(){
        if($this->isGet() && $this->isAjax()){
            $id = $this->get('id');

            $query = Order::find();

            $query->andWhere(['shop_id'=>$id]);
            $count  = $query->count();
            $order = $query->asArray()->all();

            if(!empty($order)){

                $goods_ids = array_column($order,'goods_id');
                $goods = SupplierGoods::find()
                    ->select(['id','goods_name'])
                    ->where(['id'=>$goods_ids])
                    ->asArray()
                    ->all();

                $new_goods = [];
                foreach ($goods as $item){
                    $new_goods[$item['id']] = $item['goods_name'];
                }

                foreach ($order as &$value){

                    $value['goods_name'] = '';
                    if(isset($new_goods[$value['goods_id']])&& !empty($new_goods[$value['goods_id']])){
                        $value['goods_name'] = $new_goods[$value['goods_id']];
                    }
                    if(!empty($value['complete_time'])){
                        $value['complete_time'] = date('Y-m-d H:i:s',$value['complete_time']);
                    }
                }
                unset($value);
            }
            return $this->renderPageJson($order,$count,ConstantService::$response_tab_success);
        }


    }

    /**
     * 获取收货地址列表
     * 获取收货地址列表
     * @return mixed
     * @throws \yii\base\ExitException
     */
    public function actionGetAddress(){
        if($this->isGet() && $this->isAjax()){
            $user_id = $this->get('id');
            $address=ShopAddress::find()->where(['user_id'=>$user_id])->asArray()->all();
            return $this->renderPageJson($address,count($address),ConstantService::$response_tab_success);
        }

    }

}